package org.komarichin.tags;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * User: Mykhaylo Komarichyn
 * Date: 14.01.2010
 * Time: 16:00:46
 */
public class CKEditorTag extends TagSupport {
    private static final long serialVersionUID = -2780357904282819702L;
    private static final Logger log = LoggerFactory.getLogger(CKEditorTag.class);

    private String name;
    private String config;
    private String fileBrowserBrowserUrl;
    private String fileBrowserWindowWidth;
    private String fileBrowserWindowHeight;
    private String uiColor;
    private String toolbar;
    private boolean tempBool = false;


    @Override
    public int doStartTag() throws JspException {
        if (StringUtils.isEmpty(getName())) return SKIP_BODY;
        JspWriter out = pageContext.getOut();
        try {
            out.write("<script type=\"text/javascript\" src=\"");
            out.write("/js/ckeditor/ckeditor.js");
            out.write("\"></script><script type=\"text/javascript\" src=\"");
            out.write("/js/filemanager/script.js");
            out.write("\"></script>");

            out.write("<textarea name='");
            out.write(getName());
            out.write("'>");
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        boolean isName = StringUtils.isNotEmpty(getName());
        boolean isConfig = StringUtils.isNotEmpty(getConfig());
        boolean isFileBrowserBrowserUrl = StringUtils.isNotEmpty(getFileBrowserBrowserUrl());
        boolean isFileBrowserWindowWidth = StringUtils.isNotEmpty(getFileBrowserWindowWidth());
        boolean isFileBrowserWindowHeight = StringUtils.isNotEmpty(getFileBrowserWindowHeight());
        boolean isUIColor = StringUtils.isNotEmpty(getUiColor());
        boolean isToolbar = StringUtils.isNotEmpty(getToolbar());
        boolean hasConfigurationParam = isConfig || isFileBrowserBrowserUrl || isFileBrowserWindowHeight || isFileBrowserWindowWidth || isUIColor;
        if (!isName) return SKIP_BODY;
        StringBuilder builder = new StringBuilder();
        builder.append("</textarea>");

        builder.append("<script type=\"text/javascript\">");

        builder.append("var editor").append(System.currentTimeMillis()).append(" = CKEDITOR.replace('").append(name);
        if (hasConfigurationParam) {
            builder.append("',{");
            if (isConfig)
                builConfigParam(builder, "customConfig", getConfig());

            if (isFileBrowserBrowserUrl)
                builConfigParam(builder, "filebrowserBrowseUrl", getFileBrowserBrowserUrl());

            if (isFileBrowserWindowHeight)
                builConfigParam(builder, "filebrowserImageWindowHeight", getFileBrowserWindowHeight());

            if (isFileBrowserWindowWidth)
                builConfigParam(builder, "filebrowserImageWindowWidth", getFileBrowserWindowWidth());

            if (isUIColor)
                builConfigParam(builder, "uiColor", getUiColor());

            if (isToolbar)
                builConfigParam(builder, "toolbar", getToolbar());

            builder.append("});");

        } else {
            builder.append(");");
        }

        builder.append("</script>");
        JspWriter out = pageContext.getOut();
        try {
            out.write(builder.toString());
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        tempBool = false;
        return SKIP_BODY;
    }

    private void builConfigParam(StringBuilder builder, String param, String val) {
        if (tempBool) builder.append(",");
        builder.append(param).append(":'").append(val).append("'");
        tempBool = true;
    }

    @Override
    public void release() {
        super.release();
        name = fileBrowserBrowserUrl = fileBrowserWindowHeight = fileBrowserWindowWidth = uiColor = config = toolbar = null;
        tempBool = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getConfig() {
        return config;
    }

    public void setConfig(String config) {
        this.config = config;
    }

    public String getFileBrowserBrowserUrl() {
        return fileBrowserBrowserUrl;
    }

    public void setFileBrowserBrowserUrl(String fileBrowserBrowserUrl) {
        this.fileBrowserBrowserUrl = fileBrowserBrowserUrl;
    }

    public String getFileBrowserWindowWidth() {
        return fileBrowserWindowWidth;
    }

    public void setFileBrowserWindowWidth(String fileBrowserWindowWidth) {
        this.fileBrowserWindowWidth = fileBrowserWindowWidth;
    }

    public String getFileBrowserWindowHeight() {
        return fileBrowserWindowHeight;
    }

    public void setFileBrowserWindowHeight(String fileBrowserWindowHeight) {
        this.fileBrowserWindowHeight = fileBrowserWindowHeight;
    }

    public String getUiColor() {
        return uiColor;
    }

    public void setUiColor(String uiColor) {
        this.uiColor = uiColor;
    }

    public String getToolbar() {
        return toolbar;
    }

    public void setToolbar(String toolbar) {
        this.toolbar = toolbar;
    }
}
